2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\RevisionStore
;
4 use MediaWiki\Storage\IncompleteRevisionException
;
5 use MediaWiki\Storage\RevisionRecord
;
8 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
13 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
16 * @var WikiPage $testPage
20 public function __construct( $name = null, array $data = [], $dataName = '' ) {
21 parent
::__construct( $name, $data, $dataName );
23 $this->tablesUsed
= array_merge( $this->tablesUsed
,
46 protected function setUp() {
51 $this->mergeMwGlobalArrayValue(
55 12313 => 'Dummy_talk',
59 $this->mergeMwGlobalArrayValue(
60 'wgNamespaceContentModels',
62 12312 => DummyContentForTesting
::MODEL_ID
,
66 $this->mergeMwGlobalArrayValue(
69 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
70 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
74 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
76 MWNamespace
::clearCaches();
77 // Reset namespace cache
78 $wgContLang->resetNamespaces();
80 if ( !$this->testPage
) {
82 * We have to create a new page for each subclass as the page creation may result
83 * in different DB fields being filled based on configuration.
85 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
89 protected function tearDown() {
94 MWNamespace
::clearCaches();
95 // Reset namespace cache
96 $wgContLang->resetNamespaces();
99 abstract protected function getContentHandlerUseDB();
101 private function makeRevisionWithProps( $props = null ) {
102 if ( $props === null ) {
106 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
107 $props['text'] = 'Lorem Ipsum';
110 if ( !isset( $props['user_text'] ) ) {
111 $props['user_text'] = 'Tester';
114 if ( !isset( $props['user'] ) ) {
118 if ( !isset( $props['comment'] ) ) {
119 $props['comment'] = 'just a test';
122 if ( !isset( $props['page'] ) ) {
123 $props['page'] = $this->testPage
->getId();
126 if ( !isset( $props['content_model'] ) ) {
127 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
130 $rev = new Revision( $props );
132 $dbw = wfGetDB( DB_MASTER
);
133 $rev->insertOn( $dbw );
139 * @param string $titleString
140 * @param string $text
141 * @param string|null $model
145 private function createPage( $titleString, $text, $model = null ) {
146 if ( !preg_match( '/:/', $titleString ) &&
147 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
149 $ns = $this->getDefaultWikitextNS();
150 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
153 $title = Title
::newFromText( $titleString );
154 $wikipage = new WikiPage( $title );
156 // Delete the article if it already exists
157 if ( $wikipage->exists() ) {
158 $wikipage->doDeleteArticle( "done" );
161 $content = ContentHandler
::makeContent( $text, $title, $model );
162 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
167 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
168 $this->assertNotNull( $rev, 'missing revision' );
170 $this->assertEquals( $orig->getId(), $rev->getId() );
171 $this->assertEquals( $orig->getPage(), $rev->getPage() );
172 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
173 $this->assertEquals( $orig->getUser(), $rev->getUser() );
174 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
175 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
176 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
180 * @covers Revision::getRecentChange
182 public function testGetRecentChange() {
183 $rev = $this->testPage
->getRevision();
184 $recentChange = $rev->getRecentChange();
186 // Make sure various attributes look right / the correct entry has been retrieved.
187 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
189 $rev->getTitle()->getNamespace(),
190 $recentChange->getAttribute( 'rc_namespace' )
193 $rev->getTitle()->getDBkey(),
194 $recentChange->getAttribute( 'rc_title' )
196 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
197 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
198 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
199 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
200 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
204 * @covers Revision::insertOn
206 public function testInsertOn_success() {
207 $parentId = $this->testPage
->getLatest();
209 // If an ExternalStore is set don't use it.
210 $this->setMwGlobals( 'wgDefaultExternalStore', false );
212 $rev = new Revision( [
213 'page' => $this->testPage
->getId(),
214 'title' => $this->testPage
->getTitle(),
215 'text' => 'Revision Text',
216 'comment' => 'Revision comment',
219 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
221 $this->assertInternalType( 'integer', $revId );
222 $this->assertSame( $revId, $rev->getId() );
224 // getTextId() must be an int!
225 $this->assertInternalType( 'integer', $rev->getTextId() );
227 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
229 // we currently only support storage in the text table
230 $textId = MediaWikiServices
::getInstance()
232 ->getTextIdFromAddress( $mainSlot->getAddress() );
236 [ 'old_id', 'old_text' ],
238 [ [ strval( $textId ), 'Revision Text' ] ]
253 "rev_id = {$rev->getId()}",
255 strval( $rev->getId() ),
256 strval( $this->testPage
->getId() ),
263 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
269 * @covers Revision::insertOn
271 public function testInsertOn_exceptionOnNoPage() {
272 // If an ExternalStore is set don't use it.
273 $this->setMwGlobals( 'wgDefaultExternalStore', false );
274 $this->setExpectedException(
275 IncompleteRevisionException
::class,
276 "rev_page field must not be 0!"
279 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
280 $rev = new Revision( [], 0, $title );
282 $rev->insertOn( wfGetDB( DB_MASTER
) );
286 * @covers Revision::newFromTitle
288 public function testNewFromTitle_withoutId() {
289 $latestRevId = $this->testPage
->getLatest();
291 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
293 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
294 $this->assertEquals( $latestRevId, $rev->getId() );
298 * @covers Revision::newFromTitle
300 public function testNewFromTitle_withId() {
301 $latestRevId = $this->testPage
->getLatest();
303 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
305 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
306 $this->assertEquals( $latestRevId, $rev->getId() );
310 * @covers Revision::newFromTitle
312 public function testNewFromTitle_withBadId() {
313 $latestRevId = $this->testPage
->getLatest();
315 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
317 $this->assertNull( $rev );
321 * @covers Revision::newFromRow
323 public function testNewFromRow() {
324 $orig = $this->makeRevisionWithProps();
326 $dbr = wfGetDB( DB_REPLICA
);
327 $revQuery = Revision
::getQueryInfo();
328 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
329 __METHOD__
, [], $revQuery['joins'] );
330 $this->assertTrue( is_object( $res ), 'query failed' );
332 $row = $res->fetchObject();
335 $rev = Revision
::newFromRow( $row );
337 $this->assertRevEquals( $orig, $rev );
340 public function provideNewFromArchiveRow() {
348 return $f +
[ 'ar_namespace', 'ar_title' ];
353 unset( $f['ar_text'] );
359 unset( $f['ar_text_id'] );
365 unset( $f['ar_page_id'] );
371 unset( $f['ar_parent_id'] );
377 unset( $f['ar_rev_id'] );
383 unset( $f['ar_sha1'] );
390 * @dataProvider provideNewFromArchiveRow
391 * @covers Revision::newFromArchiveRow
393 public function testNewFromArchiveRow( $selectModifier ) {
394 $services = MediaWikiServices
::getInstance();
396 $store = new RevisionStore(
397 $services->getDBLoadBalancer(),
398 $services->getService( '_SqlBlobStore' ),
399 $services->getMainWANObjectCache()
402 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
403 $this->setService( 'RevisionStore', $store );
405 $page = $this->createPage(
406 'RevisionStorageTest_testNewFromArchiveRow',
408 CONTENT_MODEL_WIKITEXT
410 $orig = $page->getRevision();
411 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
413 $dbr = wfGetDB( DB_REPLICA
);
414 $arQuery = Revision
::getArchiveQueryInfo();
415 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
417 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
418 __METHOD__
, [], $arQuery['joins']
420 $this->assertTrue( is_object( $res ), 'query failed' );
422 $row = $res->fetchObject();
425 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
426 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
427 $rev = Revision
::newFromArchiveRow( $row );
429 $this->assertRevEquals( $orig, $rev );
433 * @covers Revision::newFromArchiveRow
435 public function testNewFromArchiveRowOverrides() {
436 $page = $this->createPage(
437 'RevisionStorageTest_testNewFromArchiveRow',
439 CONTENT_MODEL_WIKITEXT
441 $orig = $page->getRevision();
442 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
444 $dbr = wfGetDB( DB_REPLICA
);
445 $arQuery = Revision
::getArchiveQueryInfo();
447 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
448 __METHOD__
, [], $arQuery['joins']
450 $this->assertTrue( is_object( $res ), 'query failed' );
452 $row = $res->fetchObject();
455 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
457 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
458 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
462 * @covers Revision::newFromId
464 public function testNewFromId() {
465 $orig = $this->testPage
->getRevision();
466 $rev = Revision
::newFromId( $orig->getId() );
467 $this->assertRevEquals( $orig, $rev );
471 * @covers Revision::newFromPageId
473 public function testNewFromPageId() {
474 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
475 $this->assertRevEquals(
476 $this->testPage
->getRevision(),
482 * @covers Revision::newFromPageId
484 public function testNewFromPageIdWithLatestId() {
485 $rev = Revision
::newFromPageId(
486 $this->testPage
->getId(),
487 $this->testPage
->getLatest()
489 $this->assertRevEquals(
490 $this->testPage
->getRevision(),
496 * @covers Revision::newFromPageId
498 public function testNewFromPageIdWithNotLatestId() {
499 $content = new WikitextContent( __METHOD__
);
500 $this->testPage
->doEditContent( $content, __METHOD__
);
501 $rev = Revision
::newFromPageId(
502 $this->testPage
->getId(),
503 $this->testPage
->getRevision()->getPrevious()->getId()
505 $this->assertRevEquals(
506 $this->testPage
->getRevision()->getPrevious(),
512 * @covers Revision::fetchRevision
514 public function testFetchRevision() {
515 // Hidden process cache assertion below
516 $this->testPage
->getRevision()->getId();
518 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
519 $id = $this->testPage
->getRevision()->getId();
521 $this->hideDeprecated( 'Revision::fetchRevision' );
522 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
524 # note: order is unspecified
526 while ( ( $row = $res->fetchObject() ) ) {
527 $rows[$row->rev_id
] = $row;
530 $this->assertEmpty( $rows, 'expected empty set' );
534 * @covers Revision::getPage
536 public function testGetPage() {
537 $page = $this->testPage
;
539 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
540 $rev = Revision
::newFromId( $orig->getId() );
542 $this->assertEquals( $page->getId(), $rev->getPage() );
546 * @covers Revision::isCurrent
548 public function testIsCurrent() {
549 $rev1 = $this->testPage
->getRevision();
551 # @todo find out if this should be true
552 # $this->assertTrue( $rev1->isCurrent() );
554 $rev1x = Revision
::newFromId( $rev1->getId() );
555 $this->assertTrue( $rev1x->isCurrent() );
557 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
558 $rev2 = $this->testPage
->getRevision();
560 # @todo find out if this should be true
561 # $this->assertTrue( $rev2->isCurrent() );
563 $rev1x = Revision
::newFromId( $rev1->getId() );
564 $this->assertFalse( $rev1x->isCurrent() );
566 $rev2x = Revision
::newFromId( $rev2->getId() );
567 $this->assertTrue( $rev2x->isCurrent() );
571 * @covers Revision::getPrevious
573 public function testGetPrevious() {
574 $oldestRevision = $this->testPage
->getOldestRevision();
575 $latestRevision = $this->testPage
->getLatest();
577 $this->assertNull( $oldestRevision->getPrevious() );
579 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
580 $newRevision = $this->testPage
->getRevision();
582 $this->assertNotNull( $newRevision->getPrevious() );
583 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
587 * @covers Revision::getNext
589 public function testGetNext() {
590 $rev1 = $this->testPage
->getRevision();
592 $this->assertNull( $rev1->getNext() );
594 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
595 $rev2 = $this->testPage
->getRevision();
597 $this->assertNotNull( $rev1->getNext() );
598 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
602 * @covers Revision::newNullRevision
604 public function testNewNullRevision() {
605 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
606 $orig = $this->testPage
->getRevision();
608 $dbw = wfGetDB( DB_MASTER
);
609 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
611 $this->assertNotEquals( $orig->getId(), $rev->getId(),
612 'new null revision should have a different id from the original revision' );
613 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
614 'new null revision should have the same text id as the original revision' );
615 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
616 'new null revision should have the same SHA1 as the original revision' );
617 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
618 'new null revision should have the same content as the original revision' );
619 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
623 * @covers Revision::insertOn
625 public function testInsertOn() {
626 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
628 $orig = $this->makeRevisionWithProps( [
632 // Make sure the revision was copied to ip_changes
633 $dbr = wfGetDB( DB_REPLICA
);
634 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
635 $row = $res->fetchObject();
637 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
639 $orig->getTimestamp(),
640 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
644 public static function provideUserWasLastToEdit() {
645 yield
'actually the last edit' => [ 3, true ];
646 yield
'not the current edit, but still by this user' => [ 2, true ];
647 yield
'edit by another user' => [ 1, false ];
648 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
652 * @covers Revision::userWasLastToEdit
653 * @dataProvider provideUserWasLastToEdit
655 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
656 $userA = User
::newFromName( "RevisionStorageTest_userA" );
657 $userB = User
::newFromName( "RevisionStorageTest_userB" );
659 if ( $userA->getId() === 0 ) {
660 $userA = User
::createNew( $userA->getName() );
663 if ( $userB->getId() === 0 ) {
664 $userB = User
::createNew( $userB->getName() );
667 $ns = $this->getDefaultWikitextNS();
669 $dbw = wfGetDB( DB_MASTER
);
672 // create revisions -----------------------------
673 $page = WikiPage
::factory( Title
::newFromText(
674 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
675 $page->insertOn( $dbw );
677 $revisions[0] = new Revision( [
678 'page' => $page->getId(),
679 // we need the title to determine the page's default content model
680 'title' => $page->getTitle(),
681 'timestamp' => '20120101000000',
682 'user' => $userA->getId(),
684 'content_model' => CONTENT_MODEL_WIKITEXT
,
685 'comment' => 'edit zero'
687 $revisions[0]->insertOn( $dbw );
689 $revisions[1] = new Revision( [
690 'page' => $page->getId(),
691 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
692 'title' => $page->getTitle(),
693 'timestamp' => '20120101000100',
694 'user' => $userA->getId(),
696 'content_model' => CONTENT_MODEL_WIKITEXT
,
697 'comment' => 'edit one'
699 $revisions[1]->insertOn( $dbw );
701 $revisions[2] = new Revision( [
702 'page' => $page->getId(),
703 'title' => $page->getTitle(),
704 'timestamp' => '20120101000200',
705 'user' => $userB->getId(),
707 'content_model' => CONTENT_MODEL_WIKITEXT
,
708 'comment' => 'edit two'
710 $revisions[2]->insertOn( $dbw );
712 $revisions[3] = new Revision( [
713 'page' => $page->getId(),
714 'title' => $page->getTitle(),
715 'timestamp' => '20120101000300',
716 'user' => $userA->getId(),
718 'content_model' => CONTENT_MODEL_WIKITEXT
,
719 'comment' => 'edit three'
721 $revisions[3]->insertOn( $dbw );
723 $revisions[4] = new Revision( [
724 'page' => $page->getId(),
725 'title' => $page->getTitle(),
726 'timestamp' => '20120101000200',
727 'user' => $userA->getId(),
729 'content_model' => CONTENT_MODEL_WIKITEXT
,
730 'comment' => 'edit four'
732 $revisions[4]->insertOn( $dbw );
734 // test it ---------------------------------
735 $since = $revisions[$sinceIdx]->getTimestamp();
737 $allRows = iterator_to_array( $dbw->select(
739 [ 'rev_id', 'rev_timestamp', 'rev_user' ],
741 'rev_page' => $page->getId(),
742 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
745 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ]
748 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
750 $this->assertEquals( $expectedLast, $wasLast );
754 * @param string $text
755 * @param string $title
756 * @param string $model
757 * @param string $format
761 private function newTestRevision( $text, $title = "Test",
762 $model = CONTENT_MODEL_WIKITEXT
, $format = null
764 if ( is_string( $title ) ) {
765 $title = Title
::newFromText( $title );
768 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
776 'content' => $content,
777 'length' => $content->getSize(),
778 'comment' => "testing",
779 'minor_edit' => false,
781 'content_format' => $format,
788 public function provideGetContentModel() {
789 // NOTE: we expect the help namespace to always contain wikitext
791 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
792 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
793 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
798 * @dataProvider provideGetContentModel
799 * @covers Revision::getContentModel
801 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
802 $rev = $this->newTestRevision( $text, $title, $model, $format );
804 $this->assertEquals( $expectedModel, $rev->getContentModel() );
807 public function provideGetContentFormat() {
808 // NOTE: we expect the help namespace to always contain wikitext
810 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
811 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
812 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
813 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
818 * @dataProvider provideGetContentFormat
819 * @covers Revision::getContentFormat
821 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
822 $rev = $this->newTestRevision( $text, $title, $model, $format );
824 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
827 public function provideGetContentHandler() {
828 // NOTE: we expect the help namespace to always contain wikitext
830 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
831 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
832 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
837 * @dataProvider provideGetContentHandler
838 * @covers Revision::getContentHandler
840 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
841 $rev = $this->newTestRevision( $text, $title, $model, $format );
843 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
846 public function provideGetContent() {
847 // NOTE: we expect the help namespace to always contain wikitext
849 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
851 serialize( 'hello world' ),
853 DummyContentForTesting
::MODEL_ID
,
855 Revision
::FOR_PUBLIC
,
856 serialize( 'hello world' )
859 serialize( 'hello world' ),
863 Revision
::FOR_PUBLIC
,
864 serialize( 'hello world' )
870 * @dataProvider provideGetContent
871 * @covers Revision::getContent
873 public function testGetContent( $text, $title, $model, $format,
874 $audience, $expectedSerialization
876 $rev = $this->newTestRevision( $text, $title, $model, $format );
877 $content = $rev->getContent( $audience );
880 $expectedSerialization,
881 is_null( $content ) ?
null : $content->serialize( $format )
886 * @covers Revision::getContent
888 public function testGetContent_failure() {
889 $rev = new Revision( [
890 'page' => $this->testPage
->getId(),
891 'content_model' => $this->testPage
->getContentModel(),
892 'text_id' => 123456789, // not in the test DB
895 MediaWiki\
suppressWarnings(); // bad text_id will trigger a warning.
897 $this->assertNull( $rev->getContent(),
898 "getContent() should return null if the revision's text blob could not be loaded." );
900 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
901 $this->assertNull( $rev->getContent(),
902 "getContent() should return null if the revision's text blob could not be loaded." );
904 MediaWiki\
suppressWarnings( 'end' );
907 public function provideGetSize() {
909 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
910 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
915 * @covers Revision::getSize
916 * @dataProvider provideGetSize
918 public function testGetSize( $text, $model, $expected_size ) {
919 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
920 $this->assertEquals( $expected_size, $rev->getSize() );
923 public function provideGetSha1() {
925 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
927 serialize( "hello world." ),
928 DummyContentForTesting
::MODEL_ID
,
929 Revision
::base36Sha1( serialize( "hello world." ) )
935 * @covers Revision::getSha1
936 * @dataProvider provideGetSha1
938 public function testGetSha1( $text, $model, $expected_hash ) {
939 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
940 $this->assertEquals( $expected_hash, $rev->getSha1() );
944 * Tests whether $rev->getContent() returns a clone when needed.
946 * @covers Revision::getContent
948 public function testGetContentClone() {
949 $content = new RevisionTestModifyableContent( "foo" );
955 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
957 'content' => $content,
958 'length' => $content->getSize(),
959 'comment' => "testing",
960 'minor_edit' => false,
964 /** @var RevisionTestModifyableContent $content */
965 $content = $rev->getContent( Revision
::RAW
);
966 $content->setText( "bar" );
968 /** @var RevisionTestModifyableContent $content2 */
969 $content2 = $rev->getContent( Revision
::RAW
);
970 // content is mutable, expect clone
971 $this->assertNotSame( $content, $content2, "expected a clone" );
972 // clone should contain the original text
973 $this->assertEquals( "foo", $content2->getText() );
975 $content2->setText( "bla bla" );
976 // clones should be independent
977 $this->assertEquals( "bar", $content->getText() );
981 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
982 * @covers Revision::getContent
984 public function testGetContentUncloned() {
985 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
986 $content = $rev->getContent( Revision
::RAW
);
987 $content2 = $rev->getContent( Revision
::RAW
);
989 // for immutable content like wikitext, this should be the same object
990 $this->assertSame( $content, $content2 );
994 * @covers Revision::loadFromId
996 public function testLoadFromId() {
997 $rev = $this->testPage
->getRevision();
998 $this->hideDeprecated( 'Revision::loadFromId' );
999 $this->assertRevEquals(
1001 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1006 * @covers Revision::loadFromPageId
1008 public function testLoadFromPageId() {
1009 $this->assertRevEquals(
1010 $this->testPage
->getRevision(),
1011 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1016 * @covers Revision::loadFromPageId
1018 public function testLoadFromPageIdWithLatestRevId() {
1019 $this->assertRevEquals(
1020 $this->testPage
->getRevision(),
1021 Revision
::loadFromPageId(
1022 wfGetDB( DB_MASTER
),
1023 $this->testPage
->getId(),
1024 $this->testPage
->getLatest()
1030 * @covers Revision::loadFromPageId
1032 public function testLoadFromPageIdWithNotLatestRevId() {
1033 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1034 $this->assertRevEquals(
1035 $this->testPage
->getRevision()->getPrevious(),
1036 Revision
::loadFromPageId(
1037 wfGetDB( DB_MASTER
),
1038 $this->testPage
->getId(),
1039 $this->testPage
->getRevision()->getPrevious()->getId()
1045 * @covers Revision::loadFromTitle
1047 public function testLoadFromTitle() {
1048 $this->assertRevEquals(
1049 $this->testPage
->getRevision(),
1050 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1055 * @covers Revision::loadFromTitle
1057 public function testLoadFromTitleWithLatestRevId() {
1058 $this->assertRevEquals(
1059 $this->testPage
->getRevision(),
1060 Revision
::loadFromTitle(
1061 wfGetDB( DB_MASTER
),
1062 $this->testPage
->getTitle(),
1063 $this->testPage
->getLatest()
1069 * @covers Revision::loadFromTitle
1071 public function testLoadFromTitleWithNotLatestRevId() {
1072 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1073 $this->assertRevEquals(
1074 $this->testPage
->getRevision()->getPrevious(),
1075 Revision
::loadFromTitle(
1076 wfGetDB( DB_MASTER
),
1077 $this->testPage
->getTitle(),
1078 $this->testPage
->getRevision()->getPrevious()->getId()
1084 * @covers Revision::loadFromTimestamp()
1086 public function testLoadFromTimestamp() {
1087 $this->assertRevEquals(
1088 $this->testPage
->getRevision(),
1089 Revision
::loadFromTimestamp(
1090 wfGetDB( DB_MASTER
),
1091 $this->testPage
->getTitle(),
1092 $this->testPage
->getRevision()->getTimestamp()
1098 * @covers Revision::getParentLengths
1100 public function testGetParentLengths_noRevIds() {
1103 Revision
::getParentLengths(
1104 wfGetDB( DB_MASTER
),
1111 * @covers Revision::getParentLengths
1113 public function testGetParentLengths_oneRevId() {
1114 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1115 $textLength = strlen( $text );
1117 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1118 $rev[1] = $this->testPage
->getLatest();
1121 [ $rev[1] => $textLength ],
1122 Revision
::getParentLengths(
1123 wfGetDB( DB_MASTER
),
1130 * @covers Revision::getParentLengths
1132 public function testGetParentLengths_multipleRevIds() {
1133 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1134 $textOneLength = strlen( $textOne );
1135 $textTwo = '831jr091jr092121j09rj1';
1136 $textTwoLength = strlen( $textTwo );
1138 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1139 $rev[1] = $this->testPage
->getLatest();
1140 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1141 $rev[2] = $this->testPage
->getLatest();
1144 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1145 Revision
::getParentLengths(
1146 wfGetDB( DB_MASTER
),
1147 [ $rev[1], $rev[2] ]
1153 * @covers Revision::getTitle
1155 public function testGetTitle_fromExistingRevision() {
1157 $this->testPage
->getTitle()->equals(
1158 $this->testPage
->getRevision()->getTitle()
1164 * @covers Revision::getTitle
1166 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1167 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1169 $this->testPage
->getTitle()->equals(
1176 * @covers Revision::isMinor
1178 public function testIsMinor_true() {
1179 // Use a sysop to ensure we can mark edits as minor
1180 $sysop = $this->getTestSysop()->getUser();
1182 $this->testPage
->doEditContent(
1183 new WikitextContent( __METHOD__
),
1189 $rev = $this->testPage
->getRevision();
1191 $this->assertSame( true, $rev->isMinor() );
1195 * @covers Revision::isMinor
1197 public function testIsMinor_false() {
1198 $this->testPage
->doEditContent(
1199 new WikitextContent( __METHOD__
),
1203 $rev = $this->testPage
->getRevision();
1205 $this->assertSame( false, $rev->isMinor() );
1209 * @covers Revision::getTimestamp
1211 public function testGetTimestamp() {
1212 $testTimestamp = wfTimestampNow();
1214 $this->testPage
->doEditContent(
1215 new WikitextContent( __METHOD__
),
1218 $rev = $this->testPage
->getRevision();
1220 $this->assertInternalType( 'string', $rev->getTimestamp() );
1221 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1222 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1226 * @covers Revision::getUser
1227 * @covers Revision::getUserText
1229 public function testGetUserAndText() {
1230 $sysop = $this->getTestSysop()->getUser();
1232 $this->testPage
->doEditContent(
1233 new WikitextContent( __METHOD__
),
1239 $rev = $this->testPage
->getRevision();
1241 $this->assertSame( $sysop->getId(), $rev->getUser() );
1242 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1246 * @covers Revision::isDeleted
1248 public function testIsDeleted_nothingDeleted() {
1249 $rev = $this->testPage
->getRevision();
1251 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1252 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1253 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1254 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1258 * @covers Revision::getVisibility
1260 public function testGetVisibility_nothingDeleted() {
1261 $rev = $this->testPage
->getRevision();
1263 $this->assertSame( 0, $rev->getVisibility() );
1267 * @covers Revision::getComment
1269 public function testGetComment_notDeleted() {
1270 $expectedSummary = 'goatlicious summary';
1272 $this->testPage
->doEditContent(
1273 new WikitextContent( __METHOD__
),
1276 $rev = $this->testPage
->getRevision();
1278 $this->assertSame( $expectedSummary, $rev->getComment() );
1282 * @covers Revision::isUnpatrolled
1284 public function testIsUnpatrolled_returnsRecentChangesId() {
1285 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1286 $rev = $this->testPage
->getRevision();
1288 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1289 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1293 * @covers Revision::isUnpatrolled
1295 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1296 // This assumes that sysops are auto patrolled
1297 $sysop = $this->getTestSysop()->getUser();
1298 $this->testPage
->doEditContent(
1299 new WikitextContent( __METHOD__
),
1305 $rev = $this->testPage
->getRevision();
1307 $this->assertSame( 0, $rev->isUnpatrolled() );
1311 * This is a simple blanket test for all simple content getters and is methods to provide some
1312 * coverage before the split of Revision into multiple classes for MCR work.
1313 * @covers Revision::getContent
1314 * @covers Revision::getSerializedData
1315 * @covers Revision::getContentModel
1316 * @covers Revision::getContentFormat
1317 * @covers Revision::getContentHandler
1319 public function testSimpleContentGetters() {
1320 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1321 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1323 $this->testPage
->doEditContent(
1324 new WikitextContent( $expectedText ),
1327 $rev = $this->testPage
->getRevision();
1329 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1330 $this->assertSame( $expectedText, $rev->getSerializedData() );
1331 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1332 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1333 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1337 * @covers Revision::newKnownCurrent
1339 public function testNewKnownCurrent() {
1340 // Setup the services
1341 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1342 $this->setService( 'MainWANObjectCache', $cache );
1343 $db = wfGetDB( DB_MASTER
);
1345 // Get a fresh revision to use during testing
1346 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1347 $rev = $this->testPage
->getRevision();
1349 // Clear any previous cache for the revision during creation
1350 $key = $cache->makeGlobalKey( 'revision-row-1.29',
1355 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1356 $this->assertFalse( $cache->get( $key ) );
1358 // Get the new revision and make sure it is in the cache and correct
1359 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1360 $this->assertRevEquals( $rev, $newRev );
1362 $cachedRow = $cache->get( $key );
1363 $this->assertNotFalse( $cachedRow );
1364 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1367 public function testNewKnownCurrent_withPageId() {
1368 $db = wfGetDB( DB_MASTER
);
1370 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1371 $rev = $this->testPage
->getRevision();
1373 $pageId = $this->testPage
->getId();
1375 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1376 $this->assertRevEquals( $rev, $newRev );
1379 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1380 $db = wfGetDB( DB_MASTER
);
1382 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1385 public function provideUserCanBitfield() {
1386 yield
[ 0, 0, [], null, true ];
1387 // Bitfields match, user has no permissions
1388 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1389 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1390 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1391 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1392 // Bitfields match, user (admin) does have permissions
1393 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1394 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1395 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1396 // Bitfields match, user (admin) does not have permissions
1397 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1398 // Bitfields match, user (oversight) does have permissions
1399 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1400 // Check permissions using the title
1402 Revision
::DELETED_TEXT
,
1403 Revision
::DELETED_TEXT
,
1405 Title
::newFromText( __METHOD__
),
1409 Revision
::DELETED_TEXT
,
1410 Revision
::DELETED_TEXT
,
1412 Title
::newFromText( __METHOD__
),
1418 * @dataProvider provideUserCanBitfield
1419 * @covers Revision::userCanBitfield
1421 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1422 $this->setMwGlobals(
1423 'wgGroupPermissions',
1426 'deletedtext' => true,
1427 'deletedhistory' => true,
1430 'viewsuppressed' => true,
1431 'suppressrevision' => true,
1435 $user = $this->getTestUser( $userGroups )->getUser();
1439 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1442 // Fallback to $wgUser
1443 $this->setMwGlobals(
1449 Revision
::userCanBitfield( $bitField, $field, null, $title )
1453 public function provideUserCan() {
1454 yield
[ 0, 0, [], true ];
1455 // Bitfields match, user has no permissions
1456 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1457 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1458 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1459 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1460 // Bitfields match, user (admin) does have permissions
1461 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1462 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1463 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1464 // Bitfields match, user (admin) does not have permissions
1465 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1466 // Bitfields match, user (oversight) does have permissions
1467 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1471 * @dataProvider provideUserCan
1472 * @covers Revision::userCan
1474 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1475 $this->setMwGlobals(
1476 'wgGroupPermissions',
1479 'deletedtext' => true,
1480 'deletedhistory' => true,
1483 'viewsuppressed' => true,
1484 'suppressrevision' => true,
1488 $user = $this->getTestUser( $userGroups )->getUser();
1489 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1493 $revision->userCan( $field, $user )